﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class alter_service : BaseForm
    {
        List<string> toAdd = new List<string>();
        List<string> toDrop = new List<string>();

        public alter_service()
            : base()
        {
        }

        public alter_service(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbService.DataSource = Utility.services(dbContext);
            cbService.DisplayMember = "Name";
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            if (cbService.SelectedIndex > 0)
                sb.AppendLine("ALTER SERVICE [" + cbService.Text + "]");
            else
                sb.AppendLine("ALTER SERVICE []");

            sb.AppendLine("ON QUEUE " + lbQueue.Text);

            sb.AppendLine("(");

            int i = 0;

            if (lbContractsToAdd.Items.Count > 0)
                foreach (string item in toAdd)
                {
                    if (i == 0)
                    {
                        sb.AppendLine("   ADD CONTRACT [" + item + "] ");
                    }
                    else
                    {
                        sb.AppendLine("  ,ADD CONTRACT [" + item + "] ");
                    }

                    i++;
                }

            if (lbContractsToDrop.Items.Count > 0)
                foreach (string item in toDrop)
                {
                    if (i == 0)
                    {
                        sb.AppendLine("   DROP CONTRACT [" + item + "] ");
                    }
                    else
                    {
                        sb.AppendLine("  ,DROP CONTRACT [" + item + "] ");
                    }

                    i++;
                }

            sb.AppendLine(")");

            tbCommand.Text = sb.ToString();
        }

        private void cbService_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbService.SelectedIndex > 0)
            {
                lbQueue.Text = Utility.queue(dbContext, cbService.Text);

                lbContractsToDrop.DataSource = Utility.contracts_in_service(dbContext, cbService.Text);
                lbContractsToDrop.DisplayMember = "Name";

                lbContractsToAdd.DataSource = Utility.contracts_not_service(dbContext, cbService.Text);
                lbContractsToAdd.DisplayMember = "Name";
            }
            else
            {
                lbContractsToDrop.Items.Clear();
                lbContractsToAdd.Items.Clear();
            }

            Write();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            toAdd.Add(lbContractsToAdd.Text);

            Write();
        }

        private void btnRemoveInitiator_Click(object sender, EventArgs e)
        {
            toDrop.Add(lbContractsToDrop.Text);

            Write();
        }
    }
}